//
//  FQProfileViewController.m
//  WeChat
//
//  Created by 方桥 on 16/5/16.
//  Copyright © 2016年 方桥. All rights reserved.
//

#import "FQProfileViewController.h"
#import "XMPPvCardTemp.h"
#import "FQEditViewController.h"
@interface FQProfileViewController ()<FQEditViewControllerDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
/**头像*/
@property (weak, nonatomic) IBOutlet UIImageView *avatarImgView;
/**昵称*/
@property (weak, nonatomic) IBOutlet UILabel *nicknameLabel;
/**微信号*/
@property (weak, nonatomic) IBOutlet UILabel *weChatNumberLabel;
/**公司*/
@property (weak, nonatomic) IBOutlet UILabel *orgNameLabel;
/**部门*/
@property (weak, nonatomic) IBOutlet UILabel *departmentLabel;
/**职位*/
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
/**电话*/
@property (weak, nonatomic) IBOutlet UILabel *telLabel;
/**email*/
@property (weak, nonatomic) IBOutlet UILabel *emailLabel;


@end

@implementation FQProfileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    XMPPvCardTemp *myVCard = [FQXmppTool sharedFQXmppTool].vCard.myvCardTemp;
    if (myVCard.photo) {
        self.avatarImgView.image = [UIImage imageWithData:myVCard.photo];
    }
    self.nicknameLabel.text = myVCard.nickname;
    self.weChatNumberLabel.text = [FQAccount shareAccount].user;
    self.orgNameLabel.text = myVCard.orgName;
    if (myVCard.orgUnits.count > 0) {
        self.departmentLabel.text = myVCard.orgUnits[0];
    }
    self.titleLabel.text = myVCard.title;
    
    self.telLabel.text = myVCard.note;
    self.emailLabel.text = myVCard.mailer;

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    switch (cell.tag) {
        case 0:
            FQLog(@"换头像");
            [self chooseImg];
            break;
        case 1:
            FQLog(@"进入下一个控制器");
            [self performSegueWithIdentifier:@"toEditVcSegue" sender:cell];
            break;
        case 2:
            FQLog(@"不做任何操作");
            break;
        default:
            break;
    }
}

-(void)chooseImg
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"请选择" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"照片库" otherButtonTitles:@"拍照", nil];
    [actionSheet showInView:self.view];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    FQLog(@"%ld",buttonIndex);
    if (buttonIndex == 2) return;
    
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    
    imagePicker.delegate = self;
    
    imagePicker.allowsEditing = YES;
    
    if (buttonIndex == 1) {//拍照
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }else{//图库
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    
    //显示控制器
    [self presentViewController:imagePicker animated:YES completion:nil];
}

#pragma mark 图片选择器的代理
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    //获取修改后的图片
    UIImage *editImage = info[UIImagePickerControllerEditedImage];
    //更改cell里的图片
    self.avatarImgView.image = editImage;
    //移除图片选择的控制器
    [self dismissViewControllerAnimated:YES completion:nil];
    //保存到服务器
    [self editViewController:nil didFinishSave:nil];
    
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    //获取目标控制器
    id destVc = segue.destinationViewController;
    //设置编辑电子名片控制器的cell属性
    if ([destVc isKindOfClass:[FQEditViewController class]]) {
        FQEditViewController *editVc = destVc;
        editVc.cell = sender;
        editVc.delegate = self;
    }
}
#pragma mark 编辑电子名片控制器的代理
-(void)editViewController:(FQEditViewController *)editVc didFinishSave:(id)sender
{
    FQLog(@"保存名片完成");
    //获取当前电子名片
    XMPPvCardTemp *myVCard = [FQXmppTool sharedFQXmppTool].vCard.myvCardTemp;
    //设置电子名片里的属性
    myVCard.photo = UIImageJPEGRepresentation(self.avatarImgView.image, 0.5);
    
    myVCard.nickname = self.nicknameLabel.text;
    
    myVCard.orgName = self.orgNameLabel.text;
    
    if (self.departmentLabel.text != nil) {
        myVCard.orgUnits = @[self.departmentLabel.text];
    }
    
    myVCard.title = self.titleLabel.text;
    
    myVCard.note = self.telLabel.text;
    
    myVCard.mailer = self.emailLabel.text;
    
    [[FQXmppTool sharedFQXmppTool].vCard updateMyvCardTemp:myVCard];
}
@end
